package com.xiaochen.sjzremind.config;

import com.xiaochen.sjzremind.service.IAmmoPriceService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.util.concurrent.TimeUnit;

/**
 * @author xc
 * @date 2025/10/09/13:18
 * @desc retrofit2配置类
 */
@Slf4j
@Configuration
public class Retrofit2Config {
    private static final String BASE_URL = "https://orzice.com/api/";

    @Bean
    public Retrofit retrofit(){
        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(new OkHttpClient.Builder()
                        .connectTimeout(30, TimeUnit.SECONDS)
                        .readTimeout(30, TimeUnit.SECONDS)
                        .writeTimeout(30, TimeUnit.SECONDS)
                        .addInterceptor(chain -> {
                            Request request = chain.request().newBuilder()
                                    .addHeader("referer", "https://orzice.com/")
                                    .addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
                                    .addHeader("accept", "application/json, text/plain, */*")
                                    .build();
                            return chain.proceed(request);
                        })
                        .build())
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
    }
    
    @Bean
    public IAmmoPriceService ammoPriceService(Retrofit retrofit){
        return retrofit.create(IAmmoPriceService.class);
    }
}
